<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;
use think\Request;

class Article extends Admin
{
    /*
     * 文章分类列表页
     * */
    public function Index()
    {
        $list = Db::name('Article')->alias('a')->join('category c', 'c.id = a.c_id', 'LEFT')->where('a.status', '<', '2')->field('a.id,a.title,a.status,a.create_time,c.name')->order('a.create_time desc')->paginate(20);
        $this->assign('list', $list);
        return $this->fetch('article_list');
    }

    /*
     * 添加文章
     * */
    public function addArticle()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');
            if (!Loader::validate('Article')->check($data)) {
                $this->error(Loader::validate('Article')->getError());
            }
            $data['create_time'] = time();
            $data['up_time'] = time();
            if (Db::name('Article')->insert($data)) {
                $this->success('添加成功', url('Article/index'));
            } else {
                $this->error('添加失败');
            }
        }
        $cate = model('Category')->getCategory(array('status' => 1));
        $this->assign('cate', $cate);
        return $this->fetch('article_add');
    }

    /*
     * 编辑文章
     * */
    public function editArticle()
    {
        $id = input('id', '', 'intval');
        if (Request::instance()->isPost()) {
            $data = input('post.');
            if (!Loader::validate('Article')->check($data)) {
                $this->error(Loader::validate('Article')->getError());
            }
            $file = Request::instance()->file('cover');
            if($file){
                $info = $file->validate(['size'=>config('upload_size'),'ext'=>'jpg,png,gif'])->move(ROOT_PATH . config('upload_path'));
                if($info){
                    $res=model('File')->saveFile($info);
                    $data['cover']=$res;
                }else{
                    $this->error($file->getError());
                }
            }else{
                unset($data['cover']);
            }
            $data['status'] = isset($data['status']) ? $data['status'] : 0;
            $data['up_time'] = time();
            if (Db::name('Article')->update($data)) {
                $this->success('修改成功', url('Article/editArticle', array('id' => $data['id'])));
            } else {
                $this->error('修改失败');
            }
        }
        $data['row'] = Db::name('Article')->find($id);
        if (!$data['row']) {
            $this->error('操作有误');
        }
        $data['cate'] = model('Category')->getCategory(array('status' => 1));
        $data['row']['cover'] = model('File')->getImg($data['row']['cover']);
        $this->assign($data);
        return $this->fetch('article_edit');
    }

    /*
     * 删除文章
     * */
    public function delArticle()
    {
        $data = input('post.', '', '');
        if (!isset($data['id'])) {
            $this->error('请选择要操作的文章');
        }
        if(is_array($data['id'])){
            $id=implode(',',$data['id']);
        }else{
            $id=$data['id'];
        }
        $flag = Db::name('Article')->where('id','in', $id)->update(['status' => 2, 'up_time' => time()]);
        if ($flag) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    /*
     * 回收站
     * */
    public function delList(){
        $list = Db::name('Article')->alias('a')->join('category c', 'c.id = a.c_id', 'LEFT')->where('a.status', '2')->field('a.id,a.title,a.up_time,c.name')->order('a.up_time desc')->paginate(20);
        $this->assign('list', $list);
        return $this->fetch('article_del');
    }
    /*
     * 撤销删除
     * */
    public function regainArticle(){
        $id = input('post.', '');
        if (!isset($id['id'])) {
            $this->error('请选择要操作的文章');
        }
        if(is_array($id['id'])){
            $id=implode(',',$id['id']);
        }else{
            $id=$id['id'];
        }
        $flag = Db::name('Article')->where('id','in', $id)->update(['status' => 1, 'up_time' => time()]);
        if ($flag) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    /*
     * 彻底删除文章
     * */
    public function del(){
        $id = input('id', '', 'intval');
        if (!$id) {
            $this->error('请选择要删除的文章');
        }
        $flag = Db::name('Article')->where('id','in', $id)->delete();
        if ($flag) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    public function test(){
        model('File')->saveFile();
    }
}
